////////////////////////////////////////////////////////////////////////////////
//
//  nino
//
////////////////////////////////////////////////////////////////////////////////

package ochart
{
	import com.adobe.serialization.json.JSON;
	
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.utils.setInterval;
	import flash.utils.setTimeout;
	
	import ochart.chartClasses.IData;
	import ochart.core.XYChart;
	import ochart.core.ChartBase;
	import ochart.core.ChartFactory;
	import ochart.core.ChartObject;
	import ochart.core.ChartObjectFactory;
	import ochart.core.FPS;
	import ochart.core.PieChart;
	import ochart.core.UpdateList;
	import ochart.core.loaders.DataLoader;
	import ochart.elements.labels.Label;
	import ochart.events.DataEvent;
	import ochart.renders.RendererFactory;
	import ochart.renders.RendererType;
	import ochart.serialization.SerializationFormat;
	import ochart.serialization.SerializeFactory;
	import ochart.utils.ChartUtil;
	import ochart.utils.DateUtil;
	import ochart.utils.logging.Logger;
	
	[SWF(frameRate="24", backgroundColor="#ffffff")]
	public class oChart extends Sprite implements IData
	{
		include "core/Version.as";
		include "chartClasses/BuildRightClickMenu.as";
		
		public static const CLASS_NAME:String = "oChart";
		
		public function get className():String
		{
			return CLASS_NAME;
		}
		
		private static var logger:Logger = Logger.getLogger(CLASS_NAME);
		
		private var _data:Object = null;
		
		public function get data():Object
		{
			return _data;
		}
		
		public function set data(value:Object):void
		{
			_data = value;
		}
		
		private var _dataFormat:String = null;
		
		public function get dataFormat():String
		{
			return _dataFormat;
		}
		
		public function set dataFormat(value:String):void
		{
			logger.info("数据格式设置为[" + value + "]");
			_dataFormat = value;
			
			if(this.serializerFactory)
			{
				this.serializerFactory.format = _dataFormat;
			}
		}
		
		private var startTime:Number = 0;
		private var finishTime:Number = 0;
		
		public var chartFactory:ChartFactory = null;
		public var chartObjectFactory:ChartObjectFactory = null;
		public var serializerFactory:SerializeFactory = null;
		public var rendererFactory:RendererFactory = null;
		
		public var chart:ChartBase = null;
		public var chartContainer:ChartObject = null;
		
		public var updateList:UpdateList = null;
		
		private var fps:FPS = null;
		private var fpsContainer:ChartObject = null;
		private var _enableFPS:Boolean = false;
		
		public function get enableFPS():Boolean
		{
			return _enableFPS;
		}
		
		public function set enableFPS(value:Boolean):void
		{
			_enableFPS = value;
			logger.info((_enableFPS ? "开启" : "关闭") + "FPS");
			
			if(_enableFPS)
			{
				fps = new FPS();
				fpsContainer.addChild(fps);
				fps.enable = value;
			}
			else
			{
				if(fps && fpsContainer.contains(fps))
				{
					fpsContainer.removeChild(fps);
				}
				fps = null;
			}
		}
			
		public function oChart()
		{
			startTime = DateUtil.time;
			
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			logger.debug("创建右键菜单");
			buildRightClickMenu();
			
			this.updateList = UpdateList.instance;
			
			logger.debug("开始初始化图表");
			this.chartContainer = new ChartObject();
			this.addChild(chartContainer);
			
			logger.debug("初始化序列化器工厂");
			this.serializerFactory = SerializeFactory.instance;
			
			logger.debug("初始化图表工厂");
			this.chartFactory = ChartFactory.instance;
			
			logger.debug("初始化图表对象工厂");
			this.chartObjectFactory = ChartObjectFactory.instance;
			
			logger.debug("初始化图表渲染器工厂");
			this.rendererFactory = RendererFactory.instance;
			this.rendererFactory.type = RendererType.VECTOR;
			
			this.fpsContainer = new ChartObject();
			this.addChild(fpsContainer);
			enableFPS = true;
			
			this.dataFormat = SerializationFormat.JSON;
			
			this.addEventListener(Event.ENTER_FRAME, onEnterFrame);
			
			this.loadData("data/data.json");
			
			finishTime = DateUtil.time;
			logger.info("初始化图表耗时[" + (finishTime - startTime) + "]毫秒");
		}
		
		public function loadData(url:String):void
		{
			logger.debug("开始加载[数据]，URL[" + url + "]");
			var dataLoader:DataLoader = new DataLoader();
			dataLoader.addEventListener(DataEvent.DATA, onLoadData);
			dataLoader.addEventListener(DataEvent.ERROR, onLoadDataError);
			dataLoader.load(url);
		}
		
		protected function onLoadData(event:DataEvent):void
		{
			logger.info("加载[数据]成功，");
			this.data = this.parseJSON(event.data.toString());
			this.buildChart();
		}
		
		protected function onLoadDataError(event:DataEvent):void
		{
			logger.error("加载[数据]失败，" + event.error);
		}
		
		/**
		 * 用于创建图表
		 */
		public function buildChart():void
		{
			var chartAlias:String = null;
			if(ChartUtil.isPieChart(this.data))
			{
				chartAlias = PieChart.ALIAS;
			}
			else
			{
				chartAlias = XYChart.ALIAS;
			}
			chart = chartFactory.create(chartAlias);
			chart.fromData(this.data);

			chartContainer.removeAllChildren();
			chartContainer.addChild(chart);
		}
		
		protected function onEnterFrame(event:Event):void
		{
			updateList.update();
		}
		
		private var swf:MovieClip = null;
		public function loadResource():void
		{
			var urlrequest:URLRequest = new URLRequest();
			urlrequest.url = "oChart_res.swf";
			var loader:Loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onResComplete);
			loader.load(urlrequest);
		}
		
		public function onResComplete(event:Event):void
		{
			swf = event.target.content;
			trace(swf.hasOwnProperty("create"));
			var xiaobai:MovieClip = swf.create("XiaoBai");
			addChild(xiaobai);
		}
		
		public function destory():void
		{
			
		}
		
		public function parseJSON(jsonString:String):Object
		{
			var json:Object = null;
			try
			{
				json = JSON.decode(jsonString);
			}
			catch(error:Error)
			{
				logger.error("JSON转换错误，错误内容：" + error.message);
			}
			return json;
		}
	}
}